package com.base.interview.devtest.api;

import com.base.interview.devtest.model.ResponseModel;
import com.base.interview.devtest.model.dto.PostDTO;
import com.base.interview.devtest.model.vo.PostVO;
import com.base.interview.devtest.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PostController implements IPostApi {
    @Autowired
    private IPostService iPostService;

    @Override
    public ResponseEntity<ResponseModel> create(@RequestBody PostDTO postDTO) {
        PostVO newPost = iPostService.createNewPost(postDTO);
        return ResponseEntity.ok(ResponseModel.success(newPost));
    }

    @Override
    public ResponseEntity<ResponseModel> listAll() {
        List<PostVO> postVOS = iPostService.listAllPosts();
        return ResponseEntity.ok(ResponseModel.success(postVOS));
    }

    @Override
    public ResponseEntity<ResponseModel> listByUser(@PathVariable(name = "userId") Long userId) {
        List<PostVO> postVOS = iPostService.listPosts(userId);
        return ResponseEntity.ok(ResponseModel.success(postVOS));
    }
}
